//
//  LBContactCell.m
//  LBContactListViewController
//
//  Created by LINAICAI on 2017/12/6.
//

#import "LBContactCell.h"
#import "LBContactModel.h"
#import <Masonry.h>
#import <LBCoreKit.h>
@implementation LBContactCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.contentView addSubview:self.selectButton];
        [self.contentView addSubview:self.headButton];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.mobileLabel];
        [self.contentView addSubview:self.verifyImageView];
        [self.contentView addSubview:self.underLineView];
        [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView).mas_equalTo(-22);
            make.centerY.equalTo(self.contentView);
        }];
        [self.headButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(55, 55));
            make.left.mas_equalTo(self.contentView).mas_equalTo(20);
            make.centerY.equalTo(self.contentView);
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headButton.mas_right).mas_offset(18);
            make.bottom.equalTo(self.contentView.mas_centerY).mas_offset(-5);
        }];
        [self.mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headButton.mas_right).mas_offset(18);
            make.top.equalTo(self.contentView.mas_centerY).mas_offset(5);
        }];
        [self.verifyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameLabel.mas_right);
            make.centerY.equalTo(self.nameLabel);
        }];
        [self.underLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).mas_offset(20);
            make.height.mas_equalTo(1/[UIScreen mainScreen].scale);
        }];

        
    }
    return self;
    
}
- (UIButton *)selectButton{
    if (!_selectButton) {
        _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_selectButton setImage:[UIImage imageNamed:@"LBContactListViewController.bundle/lb_cycle_selected"] forState:UIControlStateSelected];
        [_selectButton setImage:[UIImage imageNamed:@"LBContactListViewController.bundle/lb_cycle_unselected"] forState:UIControlStateNormal];
        [_selectButton.imageView setContentMode:UIViewContentModeScaleToFill];
        [_selectButton setSelected:YES];
        [_selectButton setHidden:NO];
    }
    return _selectButton;
}
- (UIButton *)headButton{
    if (!_headButton) {
        _headButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_headButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _headButton.layer.cornerRadius = 55.f/2.f;
        _headButton.layer.masksToBounds = YES;
    }
    return _headButton;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = [UIColor darkBlack];
        _nameLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    return _nameLabel;
}
- (UILabel *)mobileLabel{
    if (!_mobileLabel) {
        _mobileLabel = [UILabel new];
        _mobileLabel.textColor = [UIColor black];
        _mobileLabel.font = [UIFont systemFontOfSize:13];
    }
    return _mobileLabel;
}
- (UIImageView *)verifyImageView{
    if (!_verifyImageView) {
        _verifyImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LBContactListViewController.bundle/lb_user_me_verify_highlighted"]];
    }
    return _verifyImageView;
}
- (UIView *)underLineView{
    if (!_underLineView) {
        _underLineView = [[UIView alloc]init];
        _underLineView.backgroundColor = [UIColor gray];
    }
    return _underLineView;
}
- (void)setCellWithModel:(LBContactModel *)model indexPath:(NSIndexPath *)indexPath{
    NSArray *colors = @[[UIColor colorWithHexString:@"66A8F5"],
                        [UIColor colorWithHexString:@"4FC897"],
                        [UIColor colorWithHexString:@"E8AC42"],
                        [UIColor colorWithHexString:@"E85A42"]
                        ];
    if ([LBStringNotNil(model.userId) length]) {
        //已注册用户
        UIFont *font = [UIFont boldSystemFontOfSize:15];
        NSString *subStr = @" [已注册]  ";
        NSString *allStr = [model.name stringByAppendingString:subStr];
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithAttributedString:[NSMutableAttributedString lb_getAttributedStringWithString:allStr sub:subStr color:[UIColor blue]]];
        [attributedStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(allStr.length-subStr.length,subStr.length)];
        
        
        self.nameLabel.attributedText =  attributedStr;
        
    }
    else{
        //未注册用户
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:model.name];
        self.nameLabel.attributedText =  attributedStr;
    }
    if ([LBStringNotNil(model.realNameFlag) isEqualToString:@"1"]) {
        //认证用户
        self.verifyImageView.hidden = NO;
    }
    else{
        //没认证用户
        self.verifyImageView.hidden = YES;
    }
    self.mobileLabel.text = model.mobilePhone;
    //头像首汉字
    if (model.name.length) {
        NSString *firLetter = [NSString stringWithString:model.name];
        firLetter = [firLetter substringToIndex:1];
        [self.headButton setTitle:firLetter forState:UIControlStateNormal];
    }
    else{
        [self.headButton setTitle:model.firstupper forState:UIControlStateNormal];
    }
    
    WS(weakSelf);
    //如果有头像，用头像
    if ([LBStringNotNil(model.headPic) length]) {
        [self.headButton setImage:[UIImage imageNamed:@"LBContactListViewController.bundle/myIcon"] forState:UIControlStateNormal];
        [self.headButton setImage:[UIImage imageNamed:@"LBContactListViewController.bundle/myIcon"] forState:UIControlStateSelected];
        
        
        [self.headButton.imageView sd_setImageWithURL:LBIMAGEURL(model.headPic) completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            dispatch_async(dispatch_get_main_queue(),^(){
                [weakSelf.headButton setImage:image forState:UIControlStateNormal];
                [weakSelf.headButton setImage:image forState:UIControlStateSelected];
            });

        }];
    }
    //没有头像，用随机色
    else{
        [self.headButton setImage:nil forState:UIControlStateNormal];
        [self.headButton setImage:nil forState:UIControlStateSelected];
        [self.headButton setBackgroundColor:colors[indexPath.section%4]];
    }
    
}

@end
